package com.yiqi.zhiyuan.feature.certification.presenter;

import android.text.TextUtils;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.bo.UploadCertificatesInfo;
import com.yiqi.zhiyuan.feature.certification.contract.VehicleContract;
import com.yiqi.zhiyuan.feature.certification.dependencies.vehicle.DaggerVehiclePresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class VehiclePresenter implements VehicleContract.Presenter {

    private VehicleContract.View mView;

    @Inject
    VehicleContract.Model mModel;


    public VehiclePresenter(VehicleContract.View mView) {
        this.mView = mView;
        DaggerVehiclePresenterComponent.create().inject(this);
    }


    @Override
    public void start() {

    }


    @Override
    public void doInsertVehicle(List<UploadCertificatesInfo> certificatesInfo, String carNo, String carName, String carModelNo, String carColor, String id) {
        if (!ValidatorUtil.isCarnumberNO(carNo)) {
            mView.showInsertVehicleError(AM.res().getString(R.string.toast_certification_carno_error));
            return;
        }
        if (TextUtils.isEmpty(carName)) {
            mView.showInsertVehicleError(AM.res().getString(R.string.toast_certification_brand_error));
            return;
        }
        if (TextUtils.isEmpty(carModelNo)) {
            mView.showInsertVehicleError(AM.res().getString(R.string.toast_certification_type_error));
            return;
        }
        if (TextUtils.isEmpty(carColor)) {
            mView.showInsertVehicleError(AM.res().getString(R.string.toast_certification_color_error));
            return;
        }

        for (int i = 0; i < certificatesInfo.size(); i++) {
            if (TextUtils.isEmpty(certificatesInfo.get(i).getImgUrl())) {
                mView.showInsertVehicleError(String.format("请上传%s", certificatesInfo.get(i).getContent()));
                return;
            }
        }
        mModel.doInsertVehicle(certificatesInfo, carNo, carName, carModelNo, carColor, id)
                .compose(mView.<String>bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showInsertVehicleSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showInsertVehicleError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void queryAuthInfo() {

    }
}
